package simulation;

import java.util.LinkedList;
import simulation.ROBentry;

public class ROB {

	static LinkedList<ROBentry> ROB;
	static int availableEntries;
	public ROB(int ientries){
		
		availableEntries = ientries;
		ROB = new LinkedList<ROBentry>();
	}
	
	public ROBentry getHead() {
		return this.ROB.getFirst();
	}
	
	public ROBentry getTail() {
		return this.ROB.getLast();
	}
	
	public static boolean newEntry(ROBentry ientry){
		
		if(ROB.size() == availableEntries)
			return false;
		else {
			ROB.addLast(ientry);
			return true;
		}
	}
	
	public static void commit(){
		
		if(ROB.peekFirst() != null && ROB.peekFirst().flag){
			
			Simulator.registerFile[Integer.parseInt(ROB.peekFirst().register.substring(1))] = ROB.peekFirst().value; //Set register value
			
			ROB.removeFirst();
		}
	}
	
	public static void updateRegisterValue(int instructionID , int newRegisterValue) {
		
		for(int i = 0; i < ROB.size(); i++){
			if(ROB.get(i).instructionID == instructionID) {
				ROB.get(i).value = newRegisterValue;
				ROB.get(i).flag = true;
			}
		}
		
	}
	
	public static boolean isFull(){
		if(ROB.size()==availableEntries)
			return true;
			
		return false;
			
			
	}
	
	
	@Override
	public String toString(){
		
		String output = "";
		
		for(int i = 0; i < ROB.size(); i++)
		output += ROB.get(i).toString() + " \n";
		
		return output;
		
	}

}
